#include "UI_Reverb.h"

UI_Reverb::UI_Reverb(const juce::String& title,juce::AudioProcessorValueTreeState& parameters)
    : juce::Component()
{
    juce::ignoreUnused(title);

    sli_param_mix.setRange (0, 100, 1);
    sli_param_mix.setValue (0);
    sli_param_mix.setTextBoxStyle (juce::Slider::TextBoxBelow, false, 50, 20);
    sli_param_mix.onValueChange = [this]() {
        auto v = sli_param_mix.getValue();
        paramChanged(param_mix, (float)v);
    };

    sli_param_tone.setRange (0, 100, 1);
    sli_param_tone.setValue (0);
    sli_param_tone.setTextBoxStyle (juce::Slider::TextBoxBelow, false, 50, 20);
    sli_param_tone.onValueChange = [this]() {
        auto v = sli_param_tone.getValue();
        paramChanged(param_tone, (float)v);
    };

    sli_param_feedback.setRange (0, 100, 1);
    sli_param_feedback.setValue (0);
    sli_param_feedback.setTextBoxStyle (juce::Slider::TextBoxBelow, false, 50, 20);
    sli_param_feedback.onValueChange = [this]() {
        auto v = sli_param_feedback.getValue();
        paramChanged(param_feedback, (float)v);
    };

    sli_param_damp.setRange (0, 100, 1);
    sli_param_damp.setValue (0);
    sli_param_damp.setTextBoxStyle (juce::Slider::TextBoxBelow, false, 50, 20);
    sli_param_damp.onValueChange = [this]() {
        auto v = sli_param_damp.getValue();
        paramChanged(param_damp, (float)v);
    };

    lookAndFeel = new juce::LookAndFeel_V4(juce::LookAndFeel_V4::getGreyColourScheme());
    sli_param_mix.setLookAndFeel (lookAndFeel);
    sli_param_tone.setLookAndFeel (lookAndFeel);
    sli_param_feedback.setLookAndFeel (lookAndFeel);
    sli_param_damp.setLookAndFeel (lookAndFeel);

    lbl_param_mix.setJustificationType(juce::Justification::centred);
    lbl_param_tone.setJustificationType(juce::Justification::centred);
    lbl_param_feedback.setJustificationType(juce::Justification::centred);
    lbl_param_damp.setJustificationType(juce::Justification::centred);

    addAndMakeVisible (lbl_param_mix);
    addAndMakeVisible (sli_param_mix);
    addAndMakeVisible (lbl_param_tone);
    addAndMakeVisible (sli_param_tone);
    addAndMakeVisible (lbl_param_feedback);
    addAndMakeVisible (sli_param_feedback);
    addAndMakeVisible (lbl_param_damp);
    addAndMakeVisible (sli_param_damp);

    mixAttachment = std::make_unique<juce::AudioProcessorValueTreeState::SliderAttachment>(parameters, "mix", sli_param_mix);
    toneAttachment = std::make_unique<juce::AudioProcessorValueTreeState::SliderAttachment>(parameters, "tone", sli_param_tone);
    feedbackAttachment = std::make_unique<juce::AudioProcessorValueTreeState::SliderAttachment>(parameters, "feedback", sli_param_feedback);
    dampAttachment = std::make_unique<juce::AudioProcessorValueTreeState::SliderAttachment>(parameters, "damp", sli_param_damp);
    setSize(340, 200);
}


UI_Reverb::~UI_Reverb()
{
    delete lookAndFeel;
}


void UI_Reverb::paint(juce::Graphics& g)
{
    // 画背景颜色
    juce::Rectangle<float> rect = getLocalBounds().toFloat();
    g.setColour(juce::Colours::skyblue.withAlpha(0.4f));
    g.fillRoundedRectangle(rect, 8.0f);

    // g.setColour(juce::Colours::yellowgreen.withAlpha(1.0f));
    // g.drawRoundedRectangle(rect, 8.0f, 10);


}

void UI_Reverb::resized()
{
    int topbarHeight = 30;
    auto area = getLocalBounds().reduced (0);
    auto sli_width = area.getWidth() / 4;
    juce::Rectangle<int> area_topbar,area_sli,area_lbl ;
    // area_topbar = area.removeFromTop(20);
    area_lbl = area;
    area_lbl.removeFromTop(topbarHeight);
    area_lbl.setHeight((int)((float)(area.getHeight()-topbarHeight) * 0.2f));
    area_sli.setTop(area_lbl.getBottom());
    area_sli.setWidth(area.getWidth());
    area_sli.setBottom(area.getBottom());
    area_sli.removeFromBottom(5);

    lbl_param_mix.setBounds (area_lbl.removeFromLeft (sli_width));
    lbl_param_tone.setBounds (area_lbl.removeFromLeft (sli_width));
    lbl_param_feedback.setBounds (area_lbl.removeFromLeft (sli_width));
    lbl_param_damp.setBounds (area_lbl.removeFromLeft (sli_width));

    sli_param_mix.setBounds (area_sli.removeFromLeft (sli_width));
    sli_param_tone.setBounds (area_sli.removeFromLeft (sli_width));
    sli_param_feedback.setBounds (area_sli.removeFromLeft (sli_width));
    sli_param_damp.setBounds (area_sli.removeFromLeft (sli_width));
}

void UI_Reverb::mouseDown(const juce::MouseEvent& e)
{
    // juce::ignoreUnused(e);
    if (e.mods.isLeftButtonDown())
    {
        dragOffset = e.getPosition().toFloat() - juce::Point<float>(0, 0);
        toFront(true);
        isDragging = true;
    }
}

void UI_Reverb::mouseDrag(const juce::MouseEvent& e)
{
    // juce::ignoreUnused(e);
    if (isDragging && e.mods.isLeftButtonDown())
    {
        auto delta = e.getPosition().toFloat() - dragOffset;
        auto newPos = getPosition().toFloat() + delta;
        setTopLeftPosition((int)newPos.x, (int)newPos.y);
        // // 通知父组件重绘（确保连线实时刷新）
        // if (auto* parent = getParentComponent())
        //     parent->repaint();
    }
}

void UI_Reverb::mouseMove(const juce::MouseEvent& e)
{
    juce::ignoreUnused(e);
}

void UI_Reverb::mouseUp(const juce::MouseEvent& e)
{
    juce::ignoreUnused(e);
}

void UI_Reverb::mouseExit(const juce::MouseEvent& e)
{
    juce::ignoreUnused(e);
}

void UI_Reverb::paramChanged(ParamId id, float newValue)
{
    switch (id)
    {
        case param_mix:
            param_value_mix = (int)newValue;
            break;
        case param_tone:
            param_value_tone = (int)newValue;
            break;
        case param_feedback:
            param_value_feedback = (int)newValue;
            break;
        case param_damp:
            param_value_damp = (int)newValue;
            break;
        default:
            break;
    }
}

